package com.example.checkqufuface.Adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.example.checkqufuface.Base.BaseActivity;
import com.example.checkqufuface.Utils.SelfToastUtil;
import com.example.checkqufuface.Customer.BottomTicketDetailDialog;
import com.example.checkqufuface.R;
import com.example.checkqufuface.RetrufitHelp.BaseSubscriber;
import com.example.checkqufuface.RetrufitHelp.ExceptionHandle;
import com.example.checkqufuface.RetrufitHelp.RetrofitClient;
import com.example.checkqufuface.bean.QufuQueryAppOrderDetailRequsetBean;
import com.example.checkqufuface.bean.QufuQueryAppOrderDetailResultBean;
import com.example.checkqufuface.bean.QufuQueryOrderListResultBean;

import java.util.List;
import java.util.Map;

public class WidgetSearchTicketAdapter extends RecyclerView.Adapter<WidgetSearchTicketAdapter.ViewHolder> {

    private Context context;
    private List<QufuQueryOrderListResultBean.DataBean> dataList;
    private String baseUrl;
    private Map<String, String> headers;

    public WidgetSearchTicketAdapter(Context context, List<QufuQueryOrderListResultBean.DataBean> dataList, final String baseUrl, final Map<String, String> headers) {
        this.context = context;
        this.dataList = dataList;
        this.baseUrl = baseUrl;
        this.headers = headers;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.adapter_widget_ticket_list, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, final int position) {

        holder.tvId.setText(dataList.get(position).getOrderNo() + "");
//        if (dataList.get(position).getSettleStatus().equals("未支付")) {
//            holder.tvStatue.setTextColor(context.getResources().getColor(R.color.color_red));
//            holder.tvStatue.setText(dataList.get(position).getSettleStatus());
//        }else {
//            holder.tvStatue.setTextColor(context.getResources().getColor(R.color.color_green_statue));
//            holder.tvStatue.setText(dataList.get(position).getSettleStatus());
//        }


        String tackUserName = dataList.get(position).getTackUserName();
        String tackPhoneNumber = dataList.get(position).getTackPhoneNumber();
        if (tackUserName == null) {
            tackUserName = "";
        }

        if (tackPhoneNumber == null) {
            tackPhoneNumber = "";
        }
        holder.tvName.setText(tackUserName + " " + tackPhoneNumber);
        holder.tvNumber.setText("数量：" + dataList.get(position).getSaleSum());
        holder.tvTime.setText(dataList.get(position).getCreatedDate());
        if (dataList.get(position).getScenicTicketName() != null) {
            holder.tvTicketName.setText(dataList.get(position).getScenicTicketName());
        }else {
            holder.tvTicketName.setText("");
        }


        holder.rlItem.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //根据ID查询订单详情
                //先要查询是否有数据，有的话再展示
                getClickItemMessage(dataList.get(position).getOrderId());
            }
        });

    }

    private void getClickItemMessage(long orderId) {
        BaseActivity.DialogTipInShow("请稍等。。。");
        QufuQueryAppOrderDetailRequsetBean detailRequsetBean = new QufuQueryAppOrderDetailRequsetBean();
        detailRequsetBean.setOrderId(orderId);

        RetrofitClient.getInstance(context, baseUrl, headers).queryAppOrderDetail(detailRequsetBean, new BaseSubscriber<QufuQueryAppOrderDetailResultBean>(context) {
            @Override
            public void onError(ExceptionHandle.ResponeThrowable e) {
                BaseActivity.DiglogTipInDiss();
                SelfToastUtil.getInstance().show(e.message);
            }

            @Override
            public void onNext(QufuQueryAppOrderDetailResultBean qufuQueryAppOrderDetailResultBean) {
                BaseActivity.DiglogTipInDiss();
                String code = qufuQueryAppOrderDetailResultBean.getCode();
                if (code.equals("200")) {
                    BottomTicketDetailDialog bottomTicketDetailDialog = new BottomTicketDetailDialog(context,baseUrl,headers,qufuQueryAppOrderDetailResultBean.getData());
                    bottomTicketDetailDialog.show();
                }else {
                    SelfToastUtil.getInstance().show(qufuQueryAppOrderDetailResultBean.getMessage());
                }
            }
        });

    }

    @Override
    public int getItemCount() {
        return dataList.size();
    }

    public class ViewHolder extends RecyclerView.ViewHolder {

        private final TextView tvId;
        private final TextView tvStatue;
        private final TextView tvName;
        private final TextView tvTicketName;
        private final TextView tvNumber;
        private final TextView tvTime;
        private final RelativeLayout rlItem;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);

            rlItem = itemView.findViewById(R.id.tv_ticket_search_show);

            tvId = itemView.findViewById(R.id.tv_widget_ticket_id);
            tvStatue = itemView.findViewById(R.id.tv_widget_ticket_statue);
            tvName = itemView.findViewById(R.id.tv_widget_ticket_people_name);
            tvTicketName = itemView.findViewById(R.id.tv_widget_ticket_name);
            tvNumber = itemView.findViewById(R.id.tv_widget_ticket_number);
            tvTime = itemView.findViewById(R.id.tv_widget_ticket_data_time);
        }
    }
}
